﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyVideoViewerLibrary;
using System.IO;
using System.Management;
using System.Security.AccessControl;
using System.Security.Principal;


namespace MyVideoViewerConfiguration
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            cancelButton.Click += new RoutedEventHandler(cancelButton_Click);
            finishButton.Click += new RoutedEventHandler(finishButton_Click);
            nextButton.Click += new RoutedEventHandler(nextButton_Click);
            eaxBrowseButton.Click += new RoutedEventHandler(eaxBrowseButton_Click);
            myMoviesXmlBrowseButton.Click += new RoutedEventHandler(myMoviesXmlBrowseButton_Click);
            browseButton.Click += new RoutedEventHandler(browseButton_Click);

            rad1.Checked += new RoutedEventHandler(rad1_Checked);
            rad2.Checked += new RoutedEventHandler(rad1_Checked);
            rad3.Checked += new RoutedEventHandler(rad1_Checked);
            
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void myMoviesXmlBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog dg = new System.Windows.Forms.OpenFileDialog();
            dg.Filter = "XML (*.xml)|*.xml";
            if (dg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Settings.Default.MyMoviesXmlFile = dg.FileName;
            }
        }

        void browseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Settings.Default.TrailersDownloadFolder = dialog.SelectedPath;
            }
        }

        void eaxBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog dg = new System.Windows.Forms.OpenFileDialog();
            dg.Filter = "XML (*.xml)|*.xml";
            if (dg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Settings.Default.EaxMovieCatalogFile = dg.FileName;
            }
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                DataContext = Settings.Default;
                switch (Settings.Default.MovieSource)
                {
                    case MovieSource.MyMovies:
                        rad1.IsChecked = true;
                        break;
                    case MovieSource.EAX:
                        rad2.IsChecked = true;
                        break;
                    case MovieSource.MyMoviesXML:
                        rad3.IsChecked = true;
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error starting the configuration. Please check the log file.");
                File.WriteAllText("log.txt", ex.Message + ex.StackTrace);
            }
        }

        void rad1_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rad = sender as RadioButton;
            if (rad == rad1)
            {
                myMoviesGrid.Visibility = Visibility.Visible;
                eaxFileGrid.Visibility = Visibility.Collapsed;
                myMoviesFileGrid.Visibility = Visibility.Collapsed;
                myMoviesTextBox.Focus();
            }
            else if (rad == rad2)
            {
                myMoviesGrid.Visibility = Visibility.Collapsed;
                eaxFileGrid.Visibility = Visibility.Visible;
                myMoviesFileGrid.Visibility = Visibility.Collapsed;
                eaxTextBox.Focus();
            }
            else if (rad == rad3)
            {
                myMoviesGrid.Visibility = Visibility.Collapsed;
                eaxFileGrid.Visibility = Visibility.Collapsed;
                myMoviesFileGrid.Visibility = Visibility.Visible;
                myMoviesXmlTextBlock.Focus();
            }
            nextButton.Visibility = Visibility.Visible;
        }

        void nextButton_Click(object sender, RoutedEventArgs e)
        {
            if (step1Grid.Visibility == Visibility.Visible)
            {
                if (rad1.IsChecked.Value)
                {
                    if (string.IsNullOrEmpty(Settings.Default.MyMoviesServerName))
                    {
                        MessageBox.Show("Please enter the name of the computer to access the data from.");
                        return;
                    }
                    Settings.Default.MovieSource = MovieSource.MyMovies;

                    MyVideoViewerLibrary.MyMovies.MyMoviesBusiness.ResetContext();
                    if (!MyVideoViewerLibrary.MyMovies.MyMoviesBusiness.IsValid)
                    {
                        MessageBox.Show("There was an error connecting to the My Movies database, please make sure the it is installed correctly and can be accessed from this computer.");
                        return;
                    }

                    
                    if (!MyVideoViewerBusiness.IsValid)
                    {
                        MessageBox.Show("There was an error connecting to the database, please make sure 'MyVideoViewerServerSetup.exe' was run and completed successfully.");
                        return;
                    }
                }
                else if (rad2.IsChecked.Value)
                {
                    if (string.IsNullOrEmpty(Settings.Default.EaxMovieCatalogFile))
                    {
                        MessageBox.Show("Please select the EAX Movie Catalog file to use.");
                        return;
                    }
                    if (string.IsNullOrEmpty(Settings.Default.VideoViewerServerName))
                    {
                        MessageBox.Show("Please enter the name of the computer to access the data from.");
                        return;
                    }
                    Settings.Default.MovieSource = MovieSource.EAX;
                    if (!MyVideoViewerBusiness.IsValid)
                    {
                        MessageBox.Show("There was an error connecting to the database, please make sure 'MyVideoViewerServerSetup.exe' was run and completed successfully.");
                        return;
                    }
                }
                else if (rad3.IsChecked.Value)
                {
                    if (string.IsNullOrEmpty(Settings.Default.MyMoviesXmlFile))
                    {
                        MessageBox.Show("Please select the EAX Movie Catalog file to use.");
                        return;
                    }
                    if (string.IsNullOrEmpty(Settings.Default.VideoViewerServerName))
                    {
                        MessageBox.Show("Please enter the name of the computer to access the data from.");
                        return;
                    }
                    Settings.Default.MovieSource = MovieSource.MyMoviesXML;
                    if (!MyVideoViewerBusiness.IsValid)
                    {
                        MessageBox.Show("There was an error connecting to the database, please make sure 'MyVideoViewerServerSetup.exe' was run and completed successfully.");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("Please make a selection.");
                    return;
                }
                GoToStep(2);
            }
        }

        void finishButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Settings.Default.Save();
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        void GoToStep(int step)
        {
            switch (step)
            {
                case 2:
                    step1Grid.Visibility = Visibility.Collapsed;
                    step3Grid.Visibility = Visibility.Visible;
                    nextButton.Visibility = Visibility.Collapsed;
                    finishButton.Visibility = Visibility.Visible;
                    break;
                default:
                    break;
            }
        }

    }
}
